package com.qqt.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * @描述： 判断用户是否登录拦截器
 * @author koywong
 *
 */
public class SessionInterceptor extends HandlerInterceptorAdapter {

    @Override  
    public boolean preHandle(HttpServletRequest request,  
            HttpServletResponse response, Object handler) throws Exception {  
  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");  
        response.setContentType("text/html;charset=UTF-8");  
  
        // 后台session控制  

        String[] noFilters = new String[] { "captcha-image.do", "login.do" , "logout.do" };  
        String uri = request.getRequestURI();  
        System.out.println(uri);
        boolean beFilter = true;  
        for (String s : noFilters) {  
            if (uri.indexOf(s) != -1) {  
                beFilter = false;  
                break;  
            }  
        } 
        if (beFilter) {  
            Object obj = request.getSession().getAttribute("user");  
            if (null == obj) {  

                // 未登录  

//                PrintWriter out = response.getWriter();  
//                StringBuilder builder = new StringBuilder();  
//                builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");  
//
//                builder.append("window.top.location.href=\"");  
//                builder.append(request.getContextPath()+"\"");  
//                builder.append(";</script>");  
//                out.print(builder.toString());  
//                out.close();  
//                return false;  
            } else {  
                //TODO 添加日志
            }  
        }
  
        return true;  
    }

}
